/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-02-24
 * Time: 18:54
 */
public class HashBuck2 {
    static class Node {
        int key;
        int val;
        Node next;

        public Node (int key,int val) {
            this.key = key;
            this.val = val;
        }
    }

    Node[] array = new Node[10];
    private int useSized;


    public void put (int key,int val) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if (cur.key == key) {
                return;
            } else {
                cur = cur.next;
            }
        }
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        useSized++;

        double a = loadFactor(useSized,array);
        if (a >= 0.75) {
            reSize();
        }
        }


    private double loadFactor (int useSized,Node[] array) {
        return useSized * 1.0 / array.length;
    }

    private void reSize () {
        Node[] tampArr = new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null) {
                int newIndex = cur.key % tampArr.length;
                Node curNext = cur.next;
                cur.next = tampArr[newIndex];
                tampArr[newIndex] = cur;
                cur = curNext;
            }
        }
        array = tampArr;
    }

}
